package com.stepforward.me;


import java.util.ArrayList;

import java.util.Calendar;
import java.util.List;



import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;



public class AddAlarm extends Activity{
     	Button timeBtn;
	 	private Context mContext; 
    	Calendar calendar = Calendar.getInstance();
     private Notification mNotification;   
   	 private NotificationManager mNotificationManager;    
	 private final static int NOTIFICATION_ID = 0x0001;
	 int busHour,busMinute;
	 String departure,arrival,busRoute;
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.addalarm);
    		mContext = AddAlarm.this; 
        
        Bundle extras = getIntent().getExtras();
		departure = extras.getString("departure");
		arrival = extras.getString("arrival");
		String[] busInfo = extras.getString("busInfo").split("\n");
		EditText editDep = (EditText)findViewById(R.id.alarmDep);
		editDep.setText(departure);
		
		editDep = (EditText)findViewById(R.id.alarmArr);
		editDep.setText(arrival);
		
		busRoute = busInfo[0].substring(9, 11);
		editDep = (EditText)findViewById(R.id.alarmBus);  
		editDep.setText(busRoute);
		
		busHour = Integer.parseInt(busInfo[2].substring(9,10));
		busMinute = Integer.parseInt(busInfo[2].substring(11,13));
		String ampm = busInfo[2].substring(14,16);
		
		EditText bustime = (EditText) findViewById(R.id.busDepartureTime) ;
		bustime.setText(busHour+" :" + busMinute+" "+ampm);
		String getOnStop = busInfo[1].substring(17);
		TimePicker busTimePicker = (TimePicker) findViewById(R.id.busAlarmTimePick);
		busTimePicker.setIs24HourView(true);
		if (ampm.equals("pm")) 	busTimePicker.setCurrentHour(busHour+12);
		else busTimePicker.setCurrentHour(busHour);
		busTimePicker.setCurrentMinute(busMinute);

		
		ListView listview = (ListView) findViewById(R.id.busAlarmListView);
		String[] info = new String[]{busRoute+"  will leave at "+ busHour +" : " + busMinute+"  "+ampm+ " at " + getOnStop};
		ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(AddAlarm.this,
								R.layout.list_item, info);
		listview.setAdapter(listAdapter);
		Button addAlarmBtn = (Button) findViewById(R.id.btnAddalarm);
		addAlarmBtn.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				TimePicker busTimePicker = (TimePicker) findViewById(R.id.busAlarmTimePick);
				int mHour = busTimePicker.getCurrentHour();
				int mMinute = busTimePicker.getCurrentMinute();
				 calendar.setTimeInMillis(System.currentTimeMillis());
				 
				 calendar.set(Calendar.HOUR_OF_DAY, mHour);
				 calendar.set(Calendar.MINUTE, mMinute);
				
				 calendar.set(Calendar.SECOND, 0);
				 calendar.set(Calendar.MILLISECOND, 0);
				 
				Intent intent = new Intent(AddAlarm.this,AlarmReceiver.class);
				intent.putExtra("departure", departure);
				intent.putExtra("arrival",arrival);
				intent.putExtra("route",busRoute);
				intent.putExtra("hour", busHour);
				intent.putExtra("minute", busMinute);
				
				AlarmHelper alarmHelper = new AlarmHelper(mContext);
				Alarm alarm = new Alarm(departure, arrival, busHour+":"+busMinute, mHour+":"+mMinute, "Each Day");
				
				alarmHelper.insertAlarm(alarm);
				 PendingIntent pendingIntent = PendingIntent.getBroadcast(AddAlarm.this, 0, intent, 0);
				 //获取闹钟管理器
				 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
				 //设置闹钟
				 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
				 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);
				 Toast.makeText(AddAlarm.this, "Set up Alarm at "+calendar.get(Calendar.HOUR_OF_DAY)+" : "+calendar.get(Calendar.MINUTE)+"daily", Toast.LENGTH_SHORT).show();
				 //Log.d(null, "set the time to "+formatTime(h,m));
	
			}});
    }   }

